#pragma once
#include <iostream>
#include <functional>
#include <vector>

typedef std::function<void()> task_t;

void Download()
{
    std::cout << "我是一个下载任务" << ", 处理者" << getpid() << std::endl; 
}

void PrintLog()
{
    std::cout << "我是一个打印日志的任务"  << ", 处理者" << getpid() << std::endl;
}

void PushVideoStream()
{
    std::cout << "这是一个推送视频流的任务" << ", 处理者" << getpid() << std::endl;
}

class Init
{
public:
    // 任务码
    const int g_download_code = 0;
    const int g_printlog_code = 1;
    const int g_push_videostream_code = 2;
    // 任务集合
    std::vector<task_t> tasks;
public:
    Init()
    {
        tasks.push_back(Download);
        tasks.push_back(PrintLog);
        tasks.push_back(PushVideoStream);
        srand((unsigned)time(0));
    }
    bool CheckSafe(int code)
    {
        if(code >= 0 && code < tasks.size()) return true;
        else return true;
    }
    void RunTask(int code)
    {
        return tasks[code]();
    }
    int SelectTask()
    {
        return rand() % tasks.size();
    }
    std::string ToDesc(int code)
    {
        switch(code)
        {
        case 0:
            return "DownLoad";
        case 1:
            return "PrintfLog";
        case 2:
            return "PushVideoStream";
        default:
            return "None";
        }
    }
};

// 全局变量一旦运行立马初始化，调用构造函数，就将任务尾插到数组中了
Init init; // 定义对象